package com.leoman.spinningmachine.tab3;

import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.RelativeLayout;

import com.leoman.helper.util.LogUtil;
import com.leoman.helper.util.RefreshUtil;
import com.leoman.spinningmachine.R;
import com.leoman.spinningmachine.base.BaseActivity;
import com.leoman.spinningmachine.bean.LeaveGroup;
import com.leoman.spinningmachine.constant.Constant;
import com.leoman.spinningmachine.http.APIResponse;
import com.leoman.spinningmachine.http.WebServiceApi;
import com.leoman.spinningmachine.util.BGARefreshUtil;
import com.leoman.spinningmachine.util.DialogUtils;
import com.leoman.spinningmachine.util.SharedPreferencesUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

/**
 * 请假记录列表
 * Created by Administrator on 2018-05-07.
 */
public class LeaveActivity extends BaseActivity{
    @BindView(R.id.bgaRefreshLayout)
    BGARefreshLayout bgaRefreshLayout;
    @BindView(R.id.rl_nomsg)
    RelativeLayout rl_nomsg;

    @BindView(R.id.rv)
    ExpandableListView expandableListView;
    private List<LeaveGroup> lgLists = new ArrayList<>();
    private LeaveExpandableListViewAdapter adapter;
    private int page;
    private String token;
    private boolean isLastPage;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.ac_leave;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        initTitle("请假记录");

//        WebServiceApi.getInstance().leaveQueryList(SharedPreferencesUtils.getInstance().getString("token"),this,1);

        expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                v.setClickable(true);//设置点击不收回
                LogUtil.json("groupPosition:"+groupPosition);
                expandableListView.setSelectedGroup(groupPosition);
                return true;
            }
        });

        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
//                ToastUtil.showToast(LeaveActivity.this,"组："+groupPosition+"，子："+childPosition, Toast.LENGTH_SHORT);
                return false;
            }
        });

    }

    @Override
    protected void initData() {
        token  = SharedPreferencesUtils.getInstance().getString("token");

        page = 1;
        bgaRefreshLayout.setDelegate(this);
        RefreshUtil.getInstance().initRefresh(this,bgaRefreshLayout);
        bgaRefreshLayout.beginRefreshing();
    }

    @Override
    public void OnSuccessData(APIResponse apiResponse, Integer tag) {
        BGARefreshUtil.endLoading(tag,bgaRefreshLayout);
        isLastPage = BGARefreshUtil.getIsLastPage(apiResponse);
        if(page == 1){
            lgLists.clear();
        }
        lgLists.addAll(apiResponse.data.list);
        setListDatas(lgLists);
        if(lgLists!=null&&lgLists.size()>0){
            adapter = new LeaveExpandableListViewAdapter(this,lgLists);
            expandableListView.setAdapter(adapter);
            for(int i = 0;i<adapter.getGroupCount();i++){
                expandableListView.expandGroup(i);         //必须在setAdapter之后，否则空指针
            }
        }
    }

    private void setListDatas(List<LeaveGroup> leaveList) {
        if(leaveList!=null&&leaveList.size()>0){
            expandableListView.setVisibility(View.VISIBLE);
            rl_nomsg.setVisibility(View.GONE);
        }else{
            expandableListView.setVisibility(View.GONE);
            rl_nomsg.setVisibility(View.VISIBLE);
        }
    }



    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        super.onBGARefreshLayoutBeginRefreshing(refreshLayout);
        page = 1;
        WebServiceApi.getInstance().leaveQueryList(token,""+page, ""+Constant.pageSize,this,1);
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        if(isLastPage){
           return false;
        }
        page++;
        DialogUtils.getInstance().show(this);
        WebServiceApi.getInstance().leaveQueryList(token,""+page, ""+Constant.pageSize,this,2);
        return true;
    }
}
